package com.self.serviceedu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.self.servicebase.handler.MyException;
import com.self.serviceedu.entity.EduSubject;
import com.self.serviceedu.entity.excel.SubjectData;
import com.self.serviceedu.service.EduSubjectService;

import java.util.Map;

public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    public EduSubjectService eduSubjectService;

    public SubjectExcelListener() { }

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    //一行一行的读取，每一次读取有两个值，第一个值就是一级分类，第二个值就是二级分类
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if(subjectData == null){
            throw new MyException(20001,"文件数据为空~~");
        }

        //一行一行的读取，每一次读取有两个值，第一个值就是一级分类，第二个值就是二级分类
        //添加一级分类
        //判断一级分类是否重复
        EduSubject existOneSubject = this.existOneSubject(subjectData.getOneSubjectName(), eduSubjectService);
        if(existOneSubject == null){
            //没有相同的一级分类，进行添加
            existOneSubject = new EduSubject();
            existOneSubject.setTitle(subjectData.getOneSubjectName());
            existOneSubject.setParentId("0");
            eduSubjectService.save(existOneSubject);
        }

        //添加二级分类
        //判断二级分类是否重复
        //获取一级分类的ID
        String parentId = existOneSubject.getId();
        EduSubject existTwoSubject = this.exsitTwoSubject(subjectData.getTwoSubjectName(), parentId, eduSubjectService);
        if(existTwoSubject == null){
            existTwoSubject = new EduSubject();
            existTwoSubject.setTitle(subjectData.getTwoSubjectName());
            existTwoSubject.setParentId(parentId);
            eduSubjectService.save(existTwoSubject);
        }

    }

    //读取表头
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {


    }

    //完成之后做什么事
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    //判断一级分类不能重复添加(通过Subject的name查询一级分类)
    /**
     * select * from edu_subject where title=? and parent_id = ?
     * @param name
     * @param subjectService
     * @return
     */
    private EduSubject existOneSubject(String name,EduSubjectService subjectService){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }


    //判断二级分类不能重复添加(需要传入parent_id)
    private EduSubject exsitTwoSubject(String name,String pid,EduSubjectService subjectService){

        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }


}
